<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>
        原料列表
    </h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>原料分类</label>
                                    <select name="materialKind"
                                            class="form-control">
                                        <option value="板材">板材</option>
                                        <option value="型材">型材</option>
                                        <option value="" selected="selected">全部</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>类型</label>
                                    <input type="text" name="matTypeName"
                                           class="form-control"
                                           placeholder="请输入原料类型">
                                </div>
                                <div class="form-group">
                                    <label>材质</label>
                                    <input type="text" name="matQualityName"
                                           class="form-control"
                                           placeholder="请输入原料材质">
                                </div>
                                <div class="form-group">
                                    <label>规格</label>
                                    <input type="text" name="matSpecName"
                                           class="form-control"
                                           placeholder="请输入原料规格">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>宽度</label>
                                    <input type="number" step="0.1" name="matWidth" class="form-control"
                                           placeholder="请输入原料宽度">
                                </div>
                                <div class="form-group">
                                    <label>长度</label>
                                    <input type="number" step="0.1" name="matLength" class="form-control"
                                           placeholder="请输入原料长度">
                                </div>
                                <div class="form-group">
                                    <label>米重/厚度</label>
                                    <input type="number" step="0.1" name="matHeight" class="form-control"
                                           placeholder="请输入原料米重/厚度">
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">搜索</button>
                                <a shiro:hasPermission="basic:matinfo:edit" th:href="@{/basic/materials/import}"
                                   class="btn btn-warning pull-right">导入</a>
                                <a shiro:hasPermission="basic:matinfo:edit" th:href="@{/basic/materials/create}"
                                   class="btn btn-info pull-right" style="margin-right: 10px;">添加</a>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <div class="row">
                        <div class="col-md-7">
                            <table id="bootstrap-table"></table>
                        </div>
                        <div class="col-md-5">
                            <div style="position:relative;" id="right_div">
                                <form id="materialForm" class="form-horizontal">
                                    <input type="hidden" id="matId" name="matId">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="materialKind" class="col-sm-2 control-label">分类</label>
                                            <div class="col-sm-6">
                                                <select id="materialKind" class="form-control"
                                                        name="materialKind" style="width: 100%;">
                                                    <option value="">请选择</option>
                                                    <option value="型材">型材</option>
                                                    <option value="板材">板材</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matTypeId" class="col-sm-2 control-label">类型</label>
                                            <div class="col-sm-6">
                                                <select id="matTypeId" class="form-control select2"
                                                        name="matTypeId" style="width: 100%;">
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matQualityId" class="col-sm-2 control-label">材质</label>
                                            <div class="col-sm-6">
                                                <select id="matQualityId" class="form-control select2"
                                                        name="matQualityId" style="width: 100%;">
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matSpecId" class="col-sm-2 control-label">规格</label>
                                            <div class="col-sm-6">
                                                <select id="matSpecId" class="form-control select2"
                                                        name="matSpecId" style="width: 100%;">
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matCode" class="col-sm-2 control-label">编码</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" id="matCode"
                                                       name="matCode" readonly="readonly">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matWidth" class="col-sm-2 control-label">宽度</label>
                                            <div class="col-sm-10">
                                                <input type="number" step="0.1" style="padding-right: 5px;"
                                                       class="form-control" id="matWidth" name="matWidth"
                                                       autocomplete="off" placeholder="请输入宽度">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matLength" class="col-sm-2 control-label">长度</label>
                                            <div class="col-sm-10">
                                                <input type="number" step="0.1" style="padding-right: 5px;"
                                                       class="form-control" id="matLength" name="matLength"
                                                       autocomplete="off" placeholder="请输入长度">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matHeight" class="col-sm-2 control-label">米重/厚度</label>
                                            <div class="col-sm-10">
                                                <input type="number" step="0.1" style="padding-right: 5px;"
                                                       class="form-control" id="matHeight" name="matHeight"
                                                       autocomplete="off" placeholder="请输入米重/厚度">
                                            </div>
                                        </div>
                                        <!--<div class="form-group">
                                            <label for="matStockId" class="col-sm-2 control-label">区位</label>
                                            <div class="col-sm-10">
                                                <select id="matStockId" class="form-control select2"
                                                        name="matStockId" style="width: 100%;">
                                                    <option value="">请选择</option>
                                                    <option th:each="type:${materialStock}" th:value="${type.whiId}"
                                                            th:text="${type.whiName}"></option>
                                                </select>
                                            </div>
                                        </div>-->
                                        <div class="form-group">
                                            <label for="matNumUnit" class="col-sm-2 control-label">数量单位</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" id="matNumUnit"
                                                       name="matNumUnit"
                                                       placeholder="请输入数量单位">
                                            </div>
                                            <label for="matWeightUnit" class="col-sm-2 control-label">重量单位</label>
                                            <div class="col-sm-4">
                                                <input type="text" class="form-control" id="matWeightUnit"
                                                       name="matWeightUnit" placeholder="请输入重量单位">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matFormulaType" class="col-sm-2 control-label">重量公式</label>
                                            <div class="col-sm-10">
                                                <select id="matFormulaType" class="form-control select2"
                                                        name="matFormulaType"
                                                        style="width: 100%;">
                                                    <option value="">请选择</option>
                                                    <option value="">&nbsp;</option>
                                                    <option value="1">
                                                        （规格*宽度*长度*数量*7.85/1000）-（(规格-2*米重/厚度)*（宽度-2*米重/厚度）*7.85/1000*数量*长度
                                                    </option>
                                                    <option value="2">规格*规格*长度*数量*0.00617</option>
                                                    <option value="3">长度*数量*米重/厚度</option>
                                                    <option value="4">（规格-宽度）*宽度*0.02466*长度*数量</option>
                                                    <option value="5">规格*宽度*长度*2.7/1000000*数量</option>
                                                    <option value="6">规格*宽度*长度*7.85/1000000*数量</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="matSapNameDesc" class="col-sm-2 control-label">SAP物料描述</label>
                                            <div class="col-sm-10">
                                                <input type="text" style="padding-right: 5px;" class="form-control"
                                                       id="matSapNameDesc" name="matSapNameDesc" autocomplete="off"
                                                       placeholder="请输入SAP物料描述">
                                            </div>
                                        </div>
                                    </div>
                                    <div class="box-footer">
                                        <button shiro:hasPermission="basic:matinfo:edit" type="button" id="btn_edit"
                                                class="btn btn-info pull-right" disabled>
                                            修改
                                        </button>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>

<script th:inline="javascript">

    // 删除部门类型
    function updateStatus(id) {
        js.modal.confirm("确定删除这个原料名称吗？", function (result) {
            js.post({
                url: ctx + "basic/materials/del",
                data: {
                    matId: id
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        if (id == $("#matId").val()) {
                            $("#matId").val('');
                            $("#materialKind").val('');
                            $("#matTypeId").val('');
                            $("#matQualityId").val('');
                            $("#matSpecId").val('');
                            $("#matStockId").val('');
                            $("#matCode").val('');
                            $("#matLength").val('');
                            $("#matWidth").val('');
                            $("#matHeight").val('');
                            $("#matNumUnit").val('');
                            $("#matWeightUnit").val('');
                            $("#matFormulaType").val('');
                            $("#matSapNameDesc").val('');
                            $("#btn_edit").attr("disabled", true);
                        }
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            })
        })
    }

    $(function () {
        $("title").html($("title").html()+" —— 物料信息维护");
        table = js.table.init({
            url: ctx + "basic/materials/list",
            showExport: false,
            columns: [
                {
                    title: '序号', field: '', width: '40',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {title: '分类', field: 'materialKind'},
                {title: '类型', field: 'matTypeName'},
                {title: '材质', field: 'matQualityName'},
                {title: '规格', field: 'matSpecName'},
                {title: '宽度', field: 'matWidth'},
                {title: '长度', field: 'matLength'},
                {title: '米重/厚度', field: 'matHeight'},
                //{title: '区位', field: 'matStockName'},
                /*{title: '数量单位', field: 'matNumUnit'},
                {title: '重量单位', field: 'matWeightUnit'},*/
                {title: 'SAP物料描述', field: 'matSapNameDesc'},
                {
                    title: '操作', width: '60',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (hasP("basic:matinfo:edit")) {
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="updateStatus(' + row.matId + ')"> 删除</a>');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 表格搜索 */
        $("#btn-search").click(function () {
            js.table.search(table);
        });

        /** 行点击事件 */
        table.on('click-row.bs.table', function (e, row, tr) {
            $("#matId").val(row.matId);
            $("#materialKind").val(row.materialKind).trigger("change");

            js.get({
                url: ctx + "basic/materials/materialDictionary",
                data: {kindName: $('#materialKind option:selected').val()},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        $("#matTypeId").empty().append('<option value="">请选择</option>');
                        $.each(result.data.materialTypeList, function (i, v) {
                            $("#matTypeId").append('<option value="' + v.mdId + '">' + v.mdName + '</option>');
                        });
                        $("#matQualityId").empty().append('<option value="">请选择</option>');
                        $.each(result.data.materialQualityList, function (i, v) {
                            $("#matQualityId").append('<option value="' + v.mdId + '">' + v.mdName + '</option>');
                        });
                        $("#matSpecId").empty().append('<option value="">请选择</option>');
                        $.each(result.data.materialSpecList, function (i, v) {
                            $("#matSpecId").append('<option value="' + v.mdId + '">' + v.mdName + '</option>');
                        });
                    }

                    $("#matTypeId").val(row.matTypeId);
                    $("#matQualityId").val(row.matQualityId);
                    $("#matSpecId").val(row.matSpecId);
                }
            });

            $("#matStockId").val(row.matStockId).trigger("change");
            $("#matCode").val(row.matCode);
            $("#matLength").val(row.matLength);
            $("#matWidth").val(row.matWidth);
            $("#matHeight").val(row.matHeight);
            $("#matNumUnit").val(row.matNumUnit);
            $("#matWeightUnit").val(row.matWeightUnit);
            $("#matFormulaType").val(row.matFormulaType).trigger("change");
            $("#matSapNameDesc").val(row.matSapNameDesc);
            $("#btn_edit").removeAttr("disabled");
            js.validate.reset("materialForm")
        });

        /** 验证初始化 */
        js.validate.init("materialForm", {
            fields: {
                materialKind: {validators: {notEmpty: {message: '请选择分类'}}}
            }
        });

        /** 分类下拉选值改变事件 */
        $("#materialKind").change(function () {

            js.get({
                url: ctx + "basic/materials/materialDictionary",
                data: {kindName: $('#materialKind option:selected').val()},
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        $("#matTypeId").empty().append('<option value="">请选择</option>');
                        $.each(result.data.materialTypeList, function (i, v) {
                            $("#matTypeId").append('<option value="' + v.mdId + '">' + v.mdName + '</option>');
                        });
                        $("#matQualityId").empty().append('<option value="">请选择</option>');
                        $.each(result.data.materialQualityList, function (i, v) {
                            $("#matQualityId").append('<option value="' + v.mdId + '">' + v.mdName + '</option>');
                        });
                        $("#matSpecId").empty().append('<option value="">请选择</option>');
                        $.each(result.data.materialSpecList, function (i, v) {
                            $("#matSpecId").append('<option value="' + v.mdId + '">' + v.mdName + '</option>');
                        });
                    }
                }
            });
        });

        /** 修改 */
        $("#btn_edit").click(function () {

            var matTypeId = $("#matTypeId").val();
            if (matTypeId == null || matTypeId == "") {
                $.modal.warning("请选择类型！");
                js("#matTypeId").focus();
                return false;
            }

            var matQualityId = $("#matQualityId").val();
            if (matQualityId == null || matQualityId == "") {
                $.modal.warning("请选择材质！");
                js("#matQualityId").focus();
                return false;
            }

            var matSpecId = $("#matSpecId").val();
            if (matSpecId == null || matSpecId == "") {
                $.modal.warning("请选择规格！");
                js("#matSpecId").focus();
                return false;
            }

            if (js.validate.isValid("materialForm")) {
                js.post({
                    url: ctx + "basic/materials/update",
                    data: $("#materialForm").serialize(),
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            js.table.refresh(table);
                        } else {
                            js.modal.warning(result.msg);
                        }
                        js.modal.closeLoading();
                    }
                })

                /**
                 * 重置表单，重置验证
                 */
                js.validate.reset("materialForm")
            }
        });

        window.onscroll = function () {
            var top = document.body.scrollTop || document.documentElement.scrollTop;
            if (top > 100) {
                $("#right_div").css('top', top - 100);
            } else {
                $("#right_div").css('top', 0);
            }
        }
    });

</script>

</body>
</html>
